package com.kaili.basic.service.impl;

/**
 * Created by xxx on 2019-03-07.
 */


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.MessageMapper;
import com.kaili.basic.mapper.PushGroupMapper;
import com.kaili.basic.pojo.dto.MdmMessageQueryDto;
import com.kaili.basic.pojo.dto.MdmMessageUpdateDto;
import com.kaili.basic.pojo.entity.MdmMessage;
import com.kaili.basic.pojo.entity.MdmMessageExport;
import com.kaili.basic.pojo.vo.MdmMessageVO;
import com.kaili.basic.service.IMessageService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @Desc: mdm消息controller层
 * @author: dengmengna
 * @Time: 2019/7/26 9:58
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
 */
@Service
@Transactional
public class MessageServiceImpl implements IMessageService {
    /**
    *mdm消息的一个对象
    */
    @Autowired
    MessageMapper mdmMessageMapper;

    private Log log = LogFactory.getLog(this.getClass());


    /**
    *mdm推送组的一个对象
    */
    @Autowired
    PushGroupMapper mdmPushGroupMapper;

    /**
    * @Desc:  添加mdm消息
    * @author: dengmengna
    * @Time: 2019/7/26 9:59
    * @param mdmMessage
    * @return int
    */
    @Override
    public int addMdmMessage(MdmMessage mdmMessage) {
        return mdmMessageMapper.addMdmMessage(mdmMessage);
    }

    /**
    * @Desc:  删除单个mdm消息
    * @author: dengmengna
    * @Time: 2019/7/26 10:00
    * @param mdmMessage
    * @return int
    */
    @Override
    public int deleteMdmMessage(MdmMessage mdmMessage) {
        return mdmMessageMapper.deleteMdmMessage(mdmMessage);
    }

    /**
    * @Desc:  批量删除mdm消息
    * @author: dengmengna
    * @Time: 2019/7/26 10:00
    * @param map
    * @return int
    */
    @Override
    public int deleteMdmMessages(Map<String, Object> map) {
        return mdmMessageMapper.deleteMdmMessages(map);
    }

    /**
    * @Desc:  修改单个mdm消息
    * @author: dengmengna
    * @Time: 2019/7/26 10:00
    * @param mdmMessageUpdateDto
    * @return int
    */
    @Override
    public int updateMdmMessage(MdmMessageUpdateDto mdmMessageUpdateDto) {
        if(mdmMessageUpdateDto.getSendRanges()!=null){
            mdmMessageUpdateDto.setSendRange(StringUtils.join(mdmMessageUpdateDto.getSendRanges(), ","));
        }
        return mdmMessageMapper.updateMdmMessage(mdmMessageUpdateDto);
    }

    /**
    * @Desc:  批量修改mdm消息
    * @author: dengmengna
    * @Time: 2019/7/26 10:01
    * @param map
    * @return int
    */
    @Override
    public int updateMdmMessages(Map<String, Object> map) {
        return mdmMessageMapper.updateMdmMessages(map);
    }

    /**
    * @Desc:  条件查询获取mdm消息
    * @author: dengmengna
    * @Time: 2019/7/26 10:01
    * @param page
    * @param mdmMessageQueryDto
    * @return com.baomidou.mybatisplus.plugins.Page<com.kaili.basic.pojo.vo.MdmMessageVO>
    */
    @Override
    public Page<MdmMessageVO> queryMdmMessage(Page<MdmMessageVO> page, MdmMessageQueryDto mdmMessageQueryDto) {
        List<MdmMessageVO> list = mdmMessageMapper.queryMdmMessage(page,mdmMessageQueryDto);
        List<MdmMessageVO> list1 = setSendRanges(list);
        return page.setRecords(list1);
    }

    /**
    * @Desc:  查询时将数据库中字符串中的推送组转化成数组
    * @author: dengmengna
    * @Time: 2019/7/26 15:04
    * @param list
    * @return java.util.List<com.kaili.basic.pojo.vo.MdmMessageVO>
    */
    private List<MdmMessageVO>  setSendRanges(List<MdmMessageVO> list){
        List<MdmMessageVO> list1 =new ArrayList<>();
        for (MdmMessageVO mdmMessageVO: list
        ) {
            String sendRange = mdmMessageVO.getSendRange();
            if(sendRange == null){
                list1.add(mdmMessageVO);
                continue;
            }
            String sendRanges[] = sendRange.split(",");
            int i = 0;
            for (String str: sendRanges
            ) {
                if(!str.isEmpty()) i++;
            }
            String sendRangesStr ="";
            Integer sendRanges1[];
            if(i!=0){
                int j = 0;
                sendRanges1 = new Integer[i];
                for (String str: sendRanges
                ) {
                    if(!str.isEmpty()) {
                        try {
                            sendRanges1[j++] = Integer.parseInt(str);
                            sendRangesStr +=  mdmPushGroupMapper.queryPushGroupById(Integer.parseInt(str)).getPushGroupName()+",";
                        }catch (Exception e){

                        }

                    }
                }


                if(sendRangesStr.length()-1>0){
                sendRangesStr = sendRangesStr.substring(0,sendRangesStr.length()-1);
                mdmMessageVO.setSendRangeStr(sendRangesStr);
                mdmMessageVO.setSendRanges(sendRanges1);}
            }

            list1.add(mdmMessageVO);

        }
        return  list1;
    }

    /**
    * @Desc:  导出时将数据库中id字符串中的推送组转化成名称字符串
    * @author: dengmengna
    * @Time: 2019/7/26 15:13
    * @param list
    * @return java.util.List<com.kaili.basic.pojo.entity.MdmMessageExport>
    */
    private List<MdmMessageExport>  setSendRangesExport(List<MdmMessageExport> list){
        List<MdmMessageExport> list1 =new ArrayList<>();
        for (MdmMessageExport mdmMessageExport: list
        ) {
            String sendRange = mdmMessageExport.getSendRange();
            if(sendRange == null){
                list1.add(mdmMessageExport);
                continue;
            }
            String sendRanges[] = sendRange.split(",");

            int i = 0;
            for (String str: sendRanges
            ) {
                if(!str.isEmpty()) i++;
            }
            String sendRangesStr ="";
            if(i!=0){
                for (String str: sendRanges
                ) {
                    if(!str.isEmpty()) {
                        try {
                            sendRangesStr += "" + mdmPushGroupMapper.queryPushGroupById(Integer.parseInt(str)).getPushGroupName()+",";
                        }catch (Exception e){

                        }

                    }
                }
                if(sendRangesStr.length()-1>0){
                sendRangesStr = sendRangesStr.substring(0,sendRangesStr.length()-1);}
                mdmMessageExport.setSendRange(sendRangesStr);
            }

            list1.add(mdmMessageExport);

        }
        return  list1;
    }
    /**
    * @Desc:  id查询获取mdm消息信息
    * @author: dengmengna
    * @Time: 2019/7/26 10:01
    * @param id
    * @return com.kaili.basic.pojo.entity.MdmMessage
    */
    @Override
    public MdmMessageVO queryMdmMessageById(Long id) {
        List<MdmMessageVO> list = new ArrayList<>();
        MdmMessageVO mdmMessageVO = mdmMessageMapper.queryMdmMessageById(id);
        if(mdmMessageVO!=null){
        list.add(mdmMessageVO);
        List<MdmMessageVO> list1 = setSendRanges(list);
        mdmMessageVO = list1.get(0);
        }

        return mdmMessageVO;
    }

    /**
    * @Desc:  mdm消息导出
    * @author: dengmengna
    * @Time: 2019/7/26 10:02
    * @param mdmMessageQueryDto
    * @return java.util.List<com.kaili.basic.pojo.entity.MdmMessageExport>
    */
    @Override
    public List<MdmMessageExport> queryMdmMessageExport(MdmMessageQueryDto mdmMessageQueryDto) {
        List<MdmMessageExport> list = mdmMessageMapper.queryMdmMessageExport(mdmMessageQueryDto);
        List<MdmMessageExport> list1 = setSendRangesExport(list);
        return mdmMessageMapper.queryMdmMessageExport(mdmMessageQueryDto);
    }

    /**
    * @Desc:  查询消息通过sn和当前时间
    * @author: dengmengna
    * @Time: 2019/8/9 19:16
    * @param SN
    * @return java.util.List<com.kaili.basic.pojo.vo.MdmMessageVO>
    */
    @Override
    public List<MdmMessageVO> queryMessageBySN(Long SN) {
        List<MdmMessageVO> list = setSendRanges(mdmMessageMapper.queryMessageBySN(SN));
        return list;

    }

}
